/*
 * Copyright 2016 jeasonlzy(廖子尧)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed q_three_bg in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cn.kabalifeuser.callback;

import android.app.Activity;

import com.cn.kabalifeuser.callback.dialog.CallbackDialog;
import com.lzy.okgo.request.base.Request;

/**
 * Created by JFL on 2017/12/6
 * Email：WarwG1@163.com
 * function：对于网络请求是否需要弹出进度对话框
 * 自定义dialog
 */
public abstract class DialogCallback<T> extends JsonCallback<T> {

    private CallbackDialog dialog;


    private void initDialog(Activity activity) {
        dialog = new CallbackDialog(activity);
        dialog.setCancelable(false);


    }

    public DialogCallback(Activity activity) {
        super();
        if (null == activity) {

        } else {
            initDialog(activity);
        }
    }


    public DialogCallback(Activity activity, boolean isLoading) {
        super();
        if (isLoading) {
            initDialog(activity);
        }
    }


    public DialogCallback(Activity activity, boolean isLoading, String tip) {
        super();
        if (isLoading) {
//            this.tips = tip;
            initDialog(activity);
        }
    }


    @Override
    public void onStart(Request<T, ? extends Request> request) {
        if (dialog != null && !dialog.isShowing()) {
            dialog.getRotateLoading().start();
            dialog.show();
        }
    }

    @Override
    public void onFinish() {
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.getRotateLoading().stop();
            dialog.dismiss();
        }
    }
}
